package top.zhangjianyong.blog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import top.zhangjianyong.blog.enums.RespCode;
import top.zhangjianyong.blog.entity.po.TimeLine;
import top.zhangjianyong.blog.mapper.TimeLineMapper;
import top.zhangjianyong.blog.params.timeline.*;
import top.zhangjianyong.blog.response.ApiResponse;
import top.zhangjianyong.blog.service.TimeLineService;

/**
 * @author zhangjianyong
 * @since 2025/5/20 11:12
 */
@Service
public class TimeLineServiceImpl extends ServiceImpl<TimeLineMapper, TimeLine>
    implements TimeLineService {
  @Override
  public ApiResponse saveOrUpdate(TimeLineSaveOrUpdateParam param) {
    TimeLine timeLine = new TimeLine();
    BeanUtils.copyProperties(param, timeLine);
    timeLine.setStatus(0);
    return saveOrUpdate(timeLine)
        ? ApiResponse.success()
        : ApiResponse.fail(RespCode.TIME_LINE_SAVE_OR_UPDATE_ERROR);
  }

  @Override
  public ApiResponse remove(TimeLineRemoveParam param) {
    return removeById(param.id())
        ? ApiResponse.success()
        : ApiResponse.fail(RespCode.TIME_LINE_REMOVE_ERROR);
  }

  @Override
  public ApiResponse page(TimeLinePageParam param) {
    Page<TimeLine> page =
        lambdaQuery()
            .and(
                (p) ->
                    p.like(TimeLine::getTitle, param.keyword())
                        .or()
                        .like(TimeLine::getContent, param.keyword()))
            .orderByDesc(TimeLine::getDate)
            .page(new Page<>(param.pageNum(), param.pageSize()));
    return ApiResponse.success(page);
  }

  @Override
  public ApiResponse push(TimeLinePushParam param) {
    return lambdaUpdate()
            .eq(TimeLine::getId, param.id())
            .set(TimeLine::getStatus, param.status())
            .update()
        ? ApiResponse.success()
        : ApiResponse.fail(RespCode.TIME_LINE_PUSH_ERROR);
  }

  @Override
  public ApiResponse list(TimeLineListParam param) {
    List<TimeLine> list =
        lambdaQuery().eq(TimeLine::getStatus, 1).orderByDesc(TimeLine::getDate).list();
    return ApiResponse.success(list);
  }
}
